import json
import re
from openpyxl import Workbook


def write(row, column, value):
    """
    写到指定单元格中
    :param row: 行
    :param column: 列
    :param value: 值
    :return:
    """
    row += 1
    if row == 1:
        row = 2
    else:
        row += 1
    ws.cell(row, column, value)


if __name__ == '__main__':
    count = 0
    fileHandle = open('../../quotes.jl', 'r')
    wb = Workbook()
    ws = wb.active
    header = ['电影名', '电影分类', '国家', '电影时长', '上映日期', '评分']
    for k, v in enumerate(header):
        ws.cell(1, k + 1, v)

    while True:
        line = fileHandle.readline()
        if not line:
            break
        list = json.loads(line.strip())
        for k, v in enumerate(list.get('title')):
            write(k + count, 1, v)
        for k, v in enumerate(list.get('categories')):
            categories = re.findall(r".*<span>(.*?)</span>.*", v)
            write(k + count, 2, str(categories))
        for k, v in enumerate(list.get('country')):
            write(k + count, 3, v)
        for k, v in enumerate(list.get('time')):
            write(k + count, 4, v)
        for k, v in enumerate(list.get('date')):
            write(k + count, 5, v)
        for k, v in enumerate(list.get('score')):
            write(k + count, 6, v[-3:])
        count += 10
    # 保存文件
    wb.save('ssr1Data.xlsx')
